package com.icim.xmtm.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.icim.entity.Ret_prd_info;
import com.icim.util.TimeUtil;
import com.icim.xmtm.entity.CtSlimifRetProduct;
import com.icim.xmtm.entity.CtSlimifRetTrackHistory;

@Repository
public class CtSlimifRetTrackHistoryDao {
	@Resource
	private JdbcTemplate xmtmJdbcTemplate;
	
	@Resource
	private JdbcTemplate icimJdbcTemplate;

	public void saveBatch(List<CtSlimifRetTrackHistory> sheetList) {

		List<Object[]> objsList = new ArrayList<Object[]>();

		for (CtSlimifRetTrackHistory sheet : sheetList) {
			objsList.add(new Object[] { sheet.getFactoryname(),
					sheet.getEventtimekey(),
					sheet.getCus_info_snd(),
					sheet.getMtrl_prod_id_fk(),
					sheet.getMtrl_box_id_fk(),
					sheet.getPrd_seq_id(),
					sheet.getCr_tool_id_fk(),
					sheet.getEvt_timestamp(),
					sheet.getEvt_cate(),
					sheet.getEvt_usr(),
					sheet.getCf_polish_time_spend(),
					sheet.getTft_polish_time_spend(),
					sheet.getPolish_pressure2(),
					sheet.getUp_disk_frequency(),
					sheet.getDown_disk_frequency(),
					sheet.getSwing_range(),
					sheet.getAxle_distance() });
		}
		StringBuffer sql = new StringBuffer("INSERT INTO V_CT_SLIMIFRETTRACKTHISTORY_HC")
				.append("(");
		sql.append("factoryname,eventtimekey,cus_info_snd,mtrl_prod_id_fk,mtrl_box_id_fk,prd_seq_id,cr_tool_id_fk,evt_timestamp,evt_cate,");
		sql.append("evt_usr,cf_polish_time_spend,tft_polish_time_spend,polish_pressure2,up_disk_frequency,down_disk_frequency,swing_range,axle_distance");
		sql.append(")");
		sql.append("VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");

		xmtmJdbcTemplate.batchUpdate(sql.toString(), objsList);
	}
	
	
	 public CtSlimifRetTrackHistory getInforById(String prd_seq_id) {
	        String sql = "SELECT * FROM ret_pginfo_set WHERE PRD_SEQ_ID_FK = ?";
	        List<CtSlimifRetTrackHistory> infos = icimJdbcTemplate.query(sql, new String[] { prd_seq_id },
	                new RowMapper<CtSlimifRetTrackHistory>() {

	                    @Override
	                    public CtSlimifRetTrackHistory mapRow(ResultSet rs, int rowNum) throws SQLException {
	                        return getEntityFromResult(rs);
	                    }
	                });
	        return infos.size() > 0 ? infos.get(0) : new CtSlimifRetTrackHistory();
	    }

	    private CtSlimifRetTrackHistory getEntityFromResult(ResultSet rs) throws SQLException {
	    	CtSlimifRetTrackHistory sheet = new CtSlimifRetTrackHistory();
	                   sheet.setFactoryname(               rs.getString("FACTORYNAME")          );     
						sheet.setEventtimekey(             TimeUtil.getRvTime()          );     
						sheet.setCus_info_snd(              rs.getString("CUS_INFO_SND")         );     
						sheet.setMtrl_prod_id_fk(           rs.getString("MTRL_PROD_ID_FK")      );     
						sheet.setMtrl_box_id_fk(            rs.getString("MTRL_BOX_ID_FK")       );     
						sheet.setPrd_seq_id(                rs.getString("PRD_SEQ_ID_FK")           );     
						sheet.setCr_tool_id_fk(             rs.getString("CR_TOOL_ID_FK")        );     
						sheet.setEvt_timestamp(             rs.getTimestamp("EVT_TIMESTAMP")        );  
						sheet.setEvt_cate(                  rs.getString("EVT_CATE")             );     
						sheet.setEvt_usr(                   rs.getString("EVT_USR")              );     
						sheet.setCf_polish_time_spend(      rs.getString("CF_POLISH_TIME_SPEND") );     
						sheet.setTft_polish_time_spend(     rs.getString("TFT_POLISH_TIME_SPEND"));     
						sheet.setPolish_pressure2(          rs.getString("POLISH_PRESSURE2")     );     
						sheet.setUp_disk_frequency(         rs.getString("UP_DISK_FREQUENCY")    );     
						sheet.setDown_disk_frequency(       rs.getString("DOWN_DISK_FREQUENCY")  );     
						sheet.setSwing_range(               rs.getString("SWING_RANGE")          );     
						sheet.setAxle_distance(              rs.getString("AXLE_DISTANCE")       ) ;    

	        return sheet;
	    }
}
